package com.ybox.app.controller;

import com.ybox.app.domain.AppCollect;
import com.ybox.app.domain.example.AppCollectExample;
import com.ybox.app.service.AppCollectArticleService;
import com.ybox.app.service.AppCollectService;
import com.ybox.common.core.utils.uuid.SnowflakeIdWorker;
import com.ybox.common.core.web.controller.BaseController;
import com.ybox.common.core.web.domain.AjaxResult;
import com.ybox.common.core.web.page.TableDataInfo;
import com.ybox.common.log.annotation.Log;
import com.ybox.common.log.enums.BusinessType;
import com.ybox.common.security.annotation.RequiresPermissions;
import com.ybox.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 收藏夹(AppCollect)表控制层
 *
 * @author 16554
 * @since 2023-03-19 13:21:56
 */
@RestController
@RequestMapping("/collect")
public class AppCollectController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private AppCollectService appCollectService;

    @Autowired
    private AppCollectArticleService appCollectArticleService;

    /**
     * 分页查询
     *
     * @param appCollect  筛选条件
     * @return 查询结果
     */
    @RequiresPermissions("app:collect:list")
    @GetMapping
    public TableDataInfo queryByPage(AppCollect appCollect) {
        startPage();
        List<AppCollect> list = appCollectService.selectCollectList(appCollect);
        return getDataTable(list);
    }

    /**
     * 查询全部
     *
     * @param appCollect  筛选条件
     * @return 查询结果
     */
    @RequiresPermissions("app:collect:query")
    @GetMapping("/list")
    public AjaxResult queryAll(AppCollect appCollect) {
        appCollect.setuId(SecurityUtils.getUserId());
        AjaxResult ajax = AjaxResult.success(appCollectService.selectCollectList(appCollect));
        ajax.put("count",appCollectArticleService.selectCount(SecurityUtils.getUserId()));
        return ajax;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequiresPermissions("app:collect:query")
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id") String id) {
        return success(this.appCollectService.selectByPrimaryKey(id));
    }

    /**
     * 新增数据
     *
     * @param appCollect 实体
     * @return 新增结果
     */
    @RequiresPermissions("app:collect:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated(AppCollect.insert.class) @RequestBody AppCollect appCollect) {
        // 创建 Example 对象
        AppCollectExample example = new AppCollectExample();
        // Example对象包含一个static的内部类Criteria，Criteria中的方法用来定义SQL语句where后的查询条件，只能有一个createCriteria
        AppCollectExample.Criteria criteria = example.createCriteria();
        example.setDistinct(true);
        criteria.andNameEqualTo(appCollect.getName());
        criteria.andUIdEqualTo(SecurityUtils.getUserId());
        // 标题一样的而且所属用户一样的不能添加
        if (ObjectUtils.isNotEmpty(appCollectService.selectByExample(example))) {
            return error("新增收藏夹'" + appCollect.getName() + "'失败，该收藏夹已存在");
        }
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(0,0);
        appCollect.setCollectId(String.valueOf(snowflakeIdWorker.nextId()));
        appCollect.setuId(SecurityUtils.getUserId());
        appCollect.setUrl("/detail?name=" + appCollect.getName());
        appCollect.setTotalNum(0);
        appCollect.setCollectType(1);
        return toAjax(appCollectService.insertSelective(appCollect));
    }


    /**
     * 编辑数据
     *
     * @param appCollect 实体
     * @return 编辑结果
     */
    @RequiresPermissions("app:collect:edit")
    @Log(title = "收藏管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody AppCollect appCollect) {
        appCollect.setuId(SecurityUtils.getUserId());
        appCollect.setUrl("/detail?name=" + appCollect.getName());
        return toAjax(appCollectService.updateByPrimaryKeySelective(appCollect));
    }

    /**
     * 删除收藏
     * @param collectIds 收藏ids
     * @return 删除是否成功
     */
    @RequiresPermissions("app:collect:remove")
    @Log(title = "收藏管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{collectIds}")
    public AjaxResult remove(@PathVariable Long[] collectIds) {
        // 创建 Example 对象
        AppCollectExample example = new AppCollectExample();
        // Example对象包含一个static的内部类Criteria，Criteria中的方法用来定义SQL语句where后的查询条件，只能有一个createCriteria
        AppCollectExample.Criteria criteria = example.createCriteria();
        // 选择所有 id in collectIds 的收藏
        criteria.andCollectIdIn(Arrays.asList(collectIds));
        // 执行查询
        return toAjax(appCollectService.deleteByExample(example));
    }
}

